<?php
require_once __DIR__.'/admin/Bootstrap.php';
require __DIR__.'/site2/config/config.inc.php';
require_once __DIR__.'/site2/includes/CartMgr.class.php';
require_once __DIR__.'/site2/includes/OrderMgr.class.php';
require_once __DIR__.'/site2/includes/EmailMgr.class.php';

require_once(MYSQL);
$_page = new Templater(__DIR__.'/site2/views/page/page.html');

// Start the session:
if (!isset($_SESSION)) {
    session_start();
}

// The session ID is the user's cart ID:
$uid = session_id();

// Check that this is valid:
if (!isset($_SESSION['customer_id'])) 
{ 
	header("Location: checkout.php");
	exit();
}

$cartMgr= new CartMgr($dbc);
$cartMgr->clearCart($uid);

$orderMgr=new OrderMgr($dbc); 

if (!isset($_SESSION['order_id'])) 
{ 
	header("Location: checkout.php");
	exit();
}

$oid=$_SESSION['order_id'];
$_email_html = new Templater(__DIR__.'/site2/views/checkout/email_receipt.html');
$items=$orderMgr->getOrderContents($oid);
$_email_html->set("order_id",$oid);
$_email_html->set("items",$items);

$_email_plain = new Templater(__DIR__.'/site2/views/checkout/email_plain.html');
$_email_plain->set("order_id",$oid);
$_email_plain->set("items",$items);

// For Zend:
set_include_path(__DIR__.'/site2/library/');
// Include the class definition:
include ('Zend/Mail.php');
$emailMgr= new EmailMgr();
$emailMgr->SendOrderMail($_SESSION['email'], "Order #{$oid} at the delisa.", $_email_plain->parse(), $_email_html->parse());

$_SESSION = array(); 
session_destroy(); 

$_page->set('content', $_email_html->parse());
    
$_page->publish();